<template>
  <div class="container">
    <h2 class="handle-title">系统管理 > 教师管理</h2>
    <div class="handle-box">
      <div>
        <span class="search-label" style="margin-left: 5px">教师名字:</span>
        <el-input v-model="search" style="width: 120px" placeholder="请输入名字"></el-input>
        <span class="search-label" style="margin-left: 5px">教师状态:</span>
        <el-select v-model="value" style="width: 120px">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
        <el-button class="query_btn" type="primary" @click="goSearch">查询</el-button>
      </div>
    </div>
    <el-table
      :data="tableData"
      class="table"
      border>
      <el-table-column prop="username" label="名字"/>
      <el-table-column prop="usertruename" label="用户名">

        <!-- 数据可读性性处理 -->
        <template #default="{row}">
          {{ row.usertruename }}
        </template>


      </el-table-column>
      <el-table-column prop="tectitle" label="教师职称"/>
      <el-table-column label="操作" width="200">
        <template #default="{row}">
          <el-button type="text" icon="el-icon-edit-outline" @click="updateTeacherById(row.tecid)">修改</el-button>
          <el-button
            type="text"
            icon="el-icon-delete"
            class="red"
            @click="cancel(row.tecid)">离职
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 50]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next"
        :total="totalNum">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import {getTeacherList} from "../../api/public/api";
import {deleteTeacher} from "../../api/public/api";


export default {
  name: '',
  data() {
    return {
      value: "-1", //状态选择默认为 -1 表示全部
      options: [
        {
          value: '-1',
          label: '全部'
        },
        {
          value: '1',
          label: '可用'
        },
        {
          value: '0',
          label: '不可用'
        }
      ],
      tableData: [], //数据列表
      currentPage: 1, //默认第一页
      pageSize: 10, //默认每页10条
      totalNum: 0, //默认总条数
      search: "", //搜索名字
    }
  },
  mounted() {
    //获取角色管理数据初始值
    this.roleList()
  },
  methods: {
    //每页条数
    handleSizeChange(val) {
      this.pageSize = val;
      this.roleList();
    },
    // 第几页
    handleCurrentChange(val) {
      this.currentPage = val;
      this.roleList()
    },
    //封装请求方法
    roleList() {
      let params = {
        pagenum: this.currentPage,
        pagesize: this.pageSize,
        // usertruename:this.search,
        // rolestate:this.value
      }
      if (this.search) {
        params.usertruename = this.search;
      }
      getTeacherList(params).then((res) => {
        // console.log(res)
        this.tableData = res.data.pagedata.records;
        this.totalNum = res.data.pagedata.total;
      })
    },
    //Search搜索按钮
    goSearch() {
      this.roleList()
    },
    //修改
    updateTeacherById(id) {
      // console.log(id)
      this.$router.push({
        path: "/updateTeacher",
        query: {id}
      })
    },
    // 离职
    cancel(id) {
      let state = confirm("是否确认解除教师身份");
      if (state) {
        deleteTeacher(id);
        this.roleList();
      }
    }
  },
}
</script>

<style>
.handle-title {
  line-height: 50px;
}
</style>
